# -*- coding: utf-8 -*-
#===================================================
# @Time : 2021/2/19 16:49                           
# @Author : weni09                                  
# @File : adapter.py
#===================================================
from django import forms
from allauth.utils import email_address_exists
from invitations.models import  InvitationsAdapter,Invitation
from invitations import app_settings
from django.conf import settings

if hasattr(settings, 'ACCOUNT_ADAPTER'):
    if settings.ACCOUNT_ADAPTER == 'oauth.adapter.MyAccountAdapter':
        from allauth.account.adapter import DefaultAccountAdapter
        from allauth.account.signals import user_signed_up
    class MyAccountAdapter(DefaultAccountAdapter):
        def is_open_for_signup(self, request):
            if hasattr(request, 'session') and request.session.get(
                    'account_verified_email'):
                return True
            elif app_settings.INVITATION_ONLY is True:
                # Site is ONLY open for invites
                return False
            else:
                # Site is open to signup
                return True
        def get_user_signed_up_signal(self):
            return user_signed_up
        def validate_unique_email(self, email):
            if email_address_exists(email):
                raise forms.ValidationError(self.error_messages["email_taken"])
            print(Invitation.objects.filter(email=email).value("email"))
            return email
